using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_SubCatArticleList : System.Web.UI.Page
{
    #region viewstate
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.SubCategoryID + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    public string Title
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Title"] == null)
                return "";
            else
                return HttpContext.Current.Request.QueryString["Title"];
        }
    }
    public long CategoryID
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["CategoryID"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["CategoryID"]);
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
            return;
        LoadCurent();

        LoadDdlCat();
        LoadData();
        txtTitle.Text = this.Title;
        ddlCategory.SelectedValue = this.CategoryID.ToString();

    }
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulArticle = (HtmlGenericControl)Master.FindControl("ulArticle");
        ulArticle.Attributes.Add("class", "current");
        HtmlGenericControl liSubShowSubCategory = (HtmlGenericControl)Master.FindControl("liSubShowSubCategory");
        liSubShowSubCategory.Attributes.Add("class", "sub_show");
        HtmlControl divSelectArticle = (HtmlControl)Master.FindControl("divSelectArticle");
        divSelectArticle.Attributes.Add("class", "select_sub show");
    }
    #endregion


    protected void LoadDdlCat()
    {
        Criteria cri = new Criteria();
        ddlCategory.Items.Clear();
        ddlCategory.Items.Add(new ListItem("---Select---", "0"));
        ArticleCatCollection _ArticleCatCollection = ArticleCatService.GetArticleCatListByParam("[" + ArticleCat._Table + "].[" + ArticleCatColumns.Name + "]", OrderDirection.ASC, cri.Criter);
        foreach (ArticleCat _ArticleCat in _ArticleCatCollection)
        {
            ddlCategory.Items.Add(new ListItem(_ArticleCat.Name, _ArticleCat.CategoryID.ToString()));
        }
    }
    protected void LoadData()
    {
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        cri.addJoin(ArticleCat._Table, "[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.CategoryID + "]", "[" + ArticleCat._Table + "].[" + ArticleCatColumns.CategoryID + "]", Condition.EQUAL, Condition.LEFT_JOIN);
        cri.add("[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.SubcategoryLevel + "]", 0, Condition.EQUAL, Condition.AND);
        if (!string.IsNullOrEmpty(this.Title))
            cri.add("[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.Name + "]", this.Title, Condition.EQUAL, Condition.AND);
        if (this.CategoryID != long.MinValue && this.CategoryID != 0)
            cri.add("[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.CategoryID + "]", this.CategoryID, Condition.EQUAL, Condition.AND);
        ArticleSubCatCollection _ArticleSubCatCollection = ArticleSubCatService.GetArticleSubCatListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);
        rptList.DataSource = _ArticleSubCatCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;
    }
    protected void LoadData(long SubCat)
    {
        ArticleSubCat _ArticleSubCat = ArticleSubCatService.GetArticleSubCat(SubCat);
        Criteria cri = new Criteria();
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        int totalrecord = 0;
        cri.addJoin(ArticleCat._Table, "[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.CategoryID + "]", "[" + ArticleCat._Table + "].[" + ArticleCatColumns.CategoryID + "]", Condition.EQUAL, Condition.LEFT_JOIN);
        if (_ArticleSubCat != null)
        {
            cri.add("[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.SubcategoryLevel + "]", _ArticleSubCat.SubcategoryLevel + 1, Condition.EQUAL, Condition.AND);
        }
        cri.add("[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.Parrentid + "]", SubCat, Condition.EQUAL, Condition.AND);
        ArticleSubCatCollection _ArticleSubCatCollection = ArticleSubCatService.GetArticleSubCatListByParam(hidColSort, OrderDirection.ASC, hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out totalrecord);
        rptList.DataSource = _ArticleSubCatCollection;
        rptList.DataBind();
        hyperlinkPager.ItemCount = totalrecord;
        if ((float)totalrecord / (float)hyperlinkPager.PageSize <= 1)
            hyperlinkPager.Visible = false;
    }
    protected void rptList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ArticleSubCat _ArticleSubCat = (ArticleSubCat)e.Item.DataItem;
            Literal ltrNo = (Literal)e.Item.FindControl("ltrNo");
            Literal ltrName = (Literal)e.Item.FindControl("ltrName");
            TextBox txtOrder = (TextBox)e.Item.FindControl("txtOrder");
            Literal ltrCode = (Literal)e.Item.FindControl("ltrCode");
            Literal ltrParent = (Literal)e.Item.FindControl("ltrParent");
            LinkButton lbtSubCat = (LinkButton)e.Item.FindControl("lbtSubCat");
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            Label lbl_code = (Label)e.Item.FindControl("lbl_code");
            lbl_code.Text = _ArticleSubCat.Code;
            ltrNo.Text = Convert.ToString(NoCountStart + e.Item.ItemIndex);
            hidID.Value = _ArticleSubCat.SubCategoryID.ToString();
            ltrName.Text = _ArticleSubCat.Name;
            txtOrder.Text = _ArticleSubCat.Order.ToString();
            ArticleCat _ArticleCat = ArticleCatService.GetArticleCat(_ArticleSubCat.CategoryID);
            if (_ArticleCat != null)
                ltrParent.Text = _ArticleCat.Name;
            Criteria cri = new Criteria();
            cri.add("[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.Parrentid + "]", _ArticleSubCat.SubCategoryID, Condition.EQUAL, Condition.AND);
            ArticleSubCatCollection _ArticleSubCatCollection = ArticleSubCatService.GetArticleSubCatListByParam("[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.SubCategoryID + "]", OrderDirection.ASC, cri.Criter);
            if (_ArticleSubCatCollection.Count <= 0)
            {
                lbtSubCat.Visible = false;

            }
        }
    }
    protected void rptList_OnItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        object obj = new object();
        if (e.CommandName == "ViewSubcat")
        {
            obj = "[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.SubCategoryID + "]";
        }
        string commandName = e.CommandName;

        switch (commandName)
        {
            case "sortName":
                obj = "[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.Name + "]";
                break;
            case "sortOrder":
                obj = "[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.Order + "]";
                break;
            case "sortCode":
                obj = "[" + ArticleSubCat._Table + "].[" + ArticleSubCatColumns.Code + "]";
                break;
            case "sortCategory":
                obj = "[" + ArticleCat._Table + "].[" + ArticleCatColumns.Name + "]";
                break;

            default:
                break;
        }
        if (OrderDirection.ASC == OrderDirection.ASC)
            Session["Order"] = OrderDirection.DESC;
        else
            Session["Order"] = OrderDirection.ASC;
        this.hidColSort = obj.ToString();
        if ((string)e.CommandArgument != "ASC")
        {
            LoadData(long.Parse((string)e.CommandArgument));
        }
        else
        {
            LoadData();
        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        hyperlinkPager.CurrentIndex = currnetPageIndx;
        LoadData();
    }
    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            HiddenField hidID = (HiddenField)item.FindControl("hidID");
            ArticleSubCat _ArticleSubCat = ArticleSubCatService.GetArticleSubCat(long.Parse(hidID.Value));
            TextBox txtOrder = (TextBox)item.FindControl("txtOrder");
            if (_ArticleSubCat != null)
            {
                _ArticleSubCat.Order = int.Parse(txtOrder.Text.Trim());
                ArticleSubCatService.UpdateArticleSubCat(_ArticleSubCat);
            }
        }
        LoadData();
    }
    protected void btnSearchAll_OnClick(object sender, EventArgs e)
    {
        Response.Redirect("SubCategory.aspx");
    }
    protected void btnSearch_OnClick(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("SubCategory.aspx?Title={0}&CategoryID={1}", txtTitle.Text.Trim(), ddlCategory.SelectedValue));
    }
    #region Delete Account
    protected void lndelete_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rptList.Items)
        {
            CheckBox chkDelete = (CheckBox)item.FindControl("chkDelete");
            if (chkDelete.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                long ID = long.Parse(strhidID);
                //Check out Data
                Criteria cri = new Criteria();
                cri.add("[" + Article._Table + "].[" + ArticleColumns.SubCatId + "]", ID, Condition.EQUAL, Condition.AND);
                ArticleCollection _ArticleCollection = ArticleService.GetArticleListByParam("[" + Article._Table + "].[" + ArticleColumns.ArticleId + "]", OrderDirection.ASC, cri.Criter);
                if (_ArticleCollection.Count > 0)
                {
                    lb_error.Text = "Dont's delete this item because it is getting using another items";
                }
                else
                {
                    try
                    {
                        ArticleSubCatService.DeleteArticleSubCat(ID);
                    }
                    catch
                    {
                        lb_error.Text = "Dont's delete this item because it is getting using another items";
                    }
                }

            }
        }
        LoadData();
    }
    #endregion
}
